package pkg;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.google.gdata.util.ServiceException;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.extensions.When;

public class CalendarMenu {
	private static final String w = null;
	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	
	public static String printMenu(HttpSession session){
		StringBuffer out = new StringBuffer();
		if (session.getAttribute("user")!=null){
			out.append("<li id=\"menu-01\"><a href=\"calendar_index.jsp?wybor=lista\">Lista zdarzen</a></li>\n");
			out.append("<li id=\"menu-02\"><a href=\"calendar_index.jsp?wybor=nowy\">Nowe zdarzenie</a></li>\n");
		
			out.append("<BR>");
			out.append("<li id=\"menu-07\"><a href=\"regulamin.jsp\">Regulamin</a></li><li id=\"menu-08\"><a href=\"kontakt.jsp\">Kontakt</a></li>");
		}
		return out.toString(); 
	}
	
	
	public static String printView(HttpSession session, HttpServletRequest request) {
		//BaseEngine engine = (BaseEngine) session.getAttribute("Baza");
		
		StringBuffer out = new StringBuffer();
		String wybor =  request.getParameter("wybor");
		String zmiana =  request.getParameter("zmiana");
		String usun =  request.getParameter("usun");
		String poz =  request.getParameter("poz");
		
		if(wybor!=null){
			if (wybor.equals("lista"))
				return printEventList(session);
			else if(wybor.equals("nowy")){
				return showNewEventForm();
			}else if(wybor.equals("dodajnowy")){
				String begins = request.getParameter("begins");
				String ends = request.getParameter("ends");
				String title = request.getParameter("nazwa");
				try{
					DateTime start = new DateTime(dateFormat.parse(begins), TimeZone.getTimeZone("Europe/Warsaw"));
					DateTime end = new DateTime(dateFormat.parse(ends), TimeZone.getTimeZone("Europe/Warsaw"));
					Kalendarz calendar = (Kalendarz)session.getAttribute("UserCalendar");
					calendar.addNewEvent(calendar.getService(), title, start, end);
					out.append("<H2>Dodano nowe zdarzenie.</H2>\n");
					out.append("<A href=\"calendar_index.jsp?wybor=lista\">Powrot do listy zdarzen</A>\n");
				}catch (Exception e) {
					out.append(e.getMessage()+"<H2>Nie utworzono nowego zdarzenia</H2>\n");
					return out.toString();
				}
			}
		}	
		else if (zmiana!=null){
			if(zmiana.equals("zapisz")){
				String begins = request.getParameter("begins");
				String ends = request.getParameter("ends");
				String title = request.getParameter("nazwa");
				try{
					Integer position = Integer.parseInt(poz);
					DateTime start = new DateTime(dateFormat.parse(begins), TimeZone.getTimeZone("Europe/Warsaw"));
					DateTime end = new DateTime(dateFormat.parse(ends), TimeZone.getTimeZone("Europe/Warsaw"));
					Kalendarz calendar = (Kalendarz)session.getAttribute("UserCalendar");
					calendar.updateEvent(calendar.getService(), position, title, start, end);
					out.append("<H2>Zapisano zmiany.</H2>\n");
					out.append("<A href=\"calendar_index.jsp?wybor=lista\">Powrot do listy zdarzen</A>\n");
				}catch (Exception e) {
					out.append(e.getMessage()+"<H2>Nie zapisano zmian.</H2>\n");
					return out.toString();
				}
			}else if(poz!=null){
				return showChangeEventForm(session, poz);			
			}else{
				return "Blad - brak numeru zdarzenia";				
			}
		}
		else if (usun!=null){
			if(poz!=null){
				try{
					Kalendarz calendar = (Kalendarz)session.getAttribute("UserCalendar");
					Integer position = Integer.parseInt(poz);
					calendar.deleteEvent(position);
					out.append("<H2>Usunieto zdarzenie.</H2>\n");
					out.append("<A href=\"calendar_index.jsp?wybor=lista\">Powrot do listy zdarzen</A>\n");
				}catch (Exception e) {
					out.append(e.getMessage()+"<H2>Nie usunieto zdarzenia</H2>\n");
					
				}
			}
		}
		return out.toString();
	}
	
	public static String showNewEventForm(){
		StringBuffer out = new StringBuffer();
		out.append("<h2>Nowe zdarzenie</h2>\n");
		out.append("<FORM METHOD=POST ACTION=\"calendar_index.jsp?wybor=dodajnowy\">\n");
		out.append("<INPUT TYPE=text name=\"nazwa\" value=\"Nowe zdarzenie\"><BR>\n");
		out.append("<INPUT TYPE=text name=\"begins\" value=\""+DateTime.now().toUiString()+"\"><BR>\n");
		out.append("<INPUT TYPE=text name=\"ends\" value=\""+DateTime.now().toUiString()+"\"><BR>\n");
		out.append("<INPUT TYPE=submit value=\"Dodaj\"></FORM>");
		return out.toString();
	}
	
	public static String showChangeEventForm(HttpSession session, String nr){
		StringBuffer out = new StringBuffer();
		String start = DateTime.now().toUiString();
		String end = start;
		String title = "title";
		try{
			Kalendarz calendar = (Kalendarz)session.getAttribute("UserCalendar");
			Integer position = Integer.parseInt(nr);
			CalendarEventEntry originalEntry = calendar.getEvent(position);
			if(originalEntry.getTimes().size()>0){
				When time = originalEntry.getTimes().get(0);
				start = time.getStartTime().toUiString();
				end = time.getEndTime().toUiString();
				title = originalEntry.getTitle().getPlainText();
			}
		}catch (Exception e) {
			out.append(e.getMessage());
			return out.toString();
		}
		out.append("<h2>Edycja zdarzenia</h2>\n");
		out.append("<FORM METHOD=POST ACTION=\"calendar_index.jsp?zmiana=zapisz\">\n");
		out.append("<INPUT TYPE=text name=\"nazwa\" value=\""+title+"\"><BR>\n");
		out.append("<INPUT TYPE=text name=\"begins\" value=\""+start+"\"><BR>\n");
		out.append("<INPUT TYPE=text name=\"ends\" value=\""+end+"\"><BR>\n");
		out.append("<INPUT TYPE=\"hidden\" name=\"poz\" value=\""+nr+"\" />");
		out.append("<INPUT TYPE=submit value=\"Zapisz\"></FORM>");
		return out.toString();
	}
		
	public static String printEventList(HttpSession session){
		StringBuffer out = new StringBuffer();
		Kalendarz calendar = (Kalendarz)session.getAttribute("UserCalendar");
		if (calendar!=null){
			try {
				out.append("<h2>Lista zdarzen z kalendarza</h2>\n<p></p>\n");
				out.append(calendar.printAllEvents(calendar.getService(), calendar.getUser()));
			}
			catch (ServiceException e){
				return "<h2>Blad polaczenia</h2>";
			}
			catch (IOException e){
				return "<h2>Blad polaczenia</h2>";
			}
		}
		return out.toString();
	}
}
